layui.use(['form','layer','jquery'],function(){
        var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
        var $ = layui.jquery;

        $(document).on('click',"#verify_img",function(){
            $("#verify").attr("src","CaptchaServlet.do?" + Math.random());
        });

    //登录按钮
    form.on("submit(login)",function(data){
        function remove(){
            $("#code").val("");
            $("#password").val("");
            $("#password").focus();
            $("#code").parent().removeClass("layui-input-active");
        }
        $.ajax({
            url: './UserServlet.do',
            data: data.field,
            dataType: 'text',
            type: 'post',
            success: function(data) {
                console.log(data);
                var datajson = JSON.parse(data);
                if (datajson.code == 200) {
                    location.href = "index.html";
                } else if (datajson.code == 300){
                    layer.msg('登录名或密码错误');
                    remove();
                    $("#verify").attr("src","CaptchaServlet.do?" + Math.random());
                }else if (datajson.code == 400){
                    layer.msg('验证码错误');
                    remove();
                    $("#verify").attr("src","CaptchaServlet.do?" + Math.random());
                }else{
                    remove();
                    layer.msg('未知错误'+datajson.code);
                }
            },
            error:function(){
                layer.msg('未知错误请联系管理员');
            }
        })
        return false;
    })

    //表单输入效果
    $(".loginBody .input-item").click(function(e){
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    })
    $(".loginBody .layui-form-item .layui-input").focus(function(){
        $(this).parent().addClass("layui-input-focus");
    })
    $(".loginBody .layui-form-item .layui-input").blur(function(){
        $(this).parent().removeClass("layui-input-focus");
        if($(this).val() != ''){
            $(this).parent().addClass("layui-input-active");
        }else{
            $(this).parent().removeClass("layui-input-active");
        }
    })
})
